﻿Imports iConverter.Configuration
Imports System.Text




Namespace Core
    ''' <summary>
    ''' 设置管理器
    ''' </summary>
    ''' <remarks>用于保存和读取设置信息</remarks>
    Module SettingManager

        ''' <summary>
        ''' 当前设置
        ''' </summary>
        Private _settings As SettingInfo
        ''' <summary>
        ''' 获取当前设置
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property Settings() As SettingInfo
            Get
                If _settings Is Nothing Then
                    _settings = GetSettings()
                End If
                Return _settings
            End Get
        End Property


        ''' <summary>
        ''' 更新设置
        ''' </summary>
        ''' <param name="info">新的设置</param>
        ''' <remarks></remarks>
        Public Sub Update(info As SettingInfo)
            If info IsNot Nothing Then
                _settings = info.Clone

                '刷新
                EncodingManager.ClearEncodingList()

                PluginsManager.UpdateProcessers()

            End If
        End Sub

        ''' <summary>
        ''' 获取设置
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks>读取设置</remarks>
        Public Function GetSettings() As SettingInfo
            '获取设置

            My.Settings.Reload()


            '程序设置

            Dim setting As New SettingInfo

            With setting
                If My.Settings.SelectEncodings IsNot Nothing Then
                    .EncodingList = My.Settings.SelectEncodings
                End If

                .isBackupSaveDocument = My.Settings.isBackupSaveDocument
                .isUseAdvancedDestinationEncodingList = My.Settings.isUseAdvancedDestinationEncoding

                Dim cdscconverter = New System.Configuration.CommaDelimitedStringCollectionConverter()

                If Not String.IsNullOrEmpty(My.Settings.FileExtensions) Then
                    .KnowFileExtensions = cdscconverter.ConvertFromString(My.Settings.FileExtensions)
                End If

                If Not String.IsNullOrEmpty(My.Settings.NotbeOpendFileExtensions) Then
                    .NotbeOpendFileExtensions = cdscconverter.ConvertFromString(My.Settings.NotbeOpendFileExtensions)
                End If


                .isSkipUnicodeConversion = My.Settings.isSkipUnicodeConversion

                If My.Settings.DisabledPluginProviders IsNot Nothing Then
                    .DisabledPluginProviders = My.Settings.DisabledPluginProviders
                End If

                .isBatchConvertAfterAutoClose = My.Settings.isBatchConvertAfterAutoClose


                If My.Settings.PluginPostProcessings IsNot Nothing Then
                    .UsePostProcessings = My.Settings.PluginPostProcessings
                End If


                .isAutoUsePostProcessings = My.Settings.isAutoUsePostProcessings


                'TODO:增加设置成员

            End With


            '插件设置

            '插件设置管理器
            '不要从此调用 PluginManager，以免造成循环调用(2011-07-18)


            Return setting
        End Function

        ''' <summary>
        ''' 保存
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Save()
            '保存

            '程序设置

            With _settings

                My.Settings.SelectEncodings = .EncodingList

                My.Settings.DisabledPluginProviders = .DisabledPluginProviders

                My.Settings.PluginPostProcessings = .UsePostProcessings


                My.Settings.isBackupSaveDocument = .isBackupSaveDocument
                My.Settings.isUseAdvancedDestinationEncoding = .isUseAdvancedDestinationEncodingList

                My.Settings.isSkipUnicodeConversion = .isSkipUnicodeConversion

                My.Settings.isBatchConvertAfterAutoClose = .isBatchConvertAfterAutoClose

                My.Settings.isAutoUsePostProcessings = .isAutoUsePostProcessings


                'TODO:增加设置成员

            End With



            '插件设置
            My.Settings.Save()
        End Sub

        ''' <summary>
        ''' 重置
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Reset()


            '程序设置
            My.Settings.Reset()

            '插件设置


            PluginsManager.ResetAllSettings()


            Refresh()

        End Sub

        ''' <summary>
        ''' 刷新
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Refresh()
            _settings = Nothing
            _settings = GetSettings()

        End Sub


    End Module

End Namespace

